/*
 * Copyright 2014 by État de Genève (Switzerland)
 * All rights reserved.
 *
 * Author: ELCA Informatique SA
 */
define(function () {
    return {
        init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
            var callbackFunc = ko.unwrap(valueAccessor());

            var $el = $(element);

            // if element is not a input field, find all input fields inside the element and bind to them
            if($el.prop('tagName') !== 'INPUT') {
                $el = $el.find('input[type="text"]');
            }

            // bind on key up to let a chance for validated fields (like dates) to update
            $el.keyup(function (event) {
                    if (event.which === 13) {
                        callbackFunc.call(bindingContext.$root, element);
                    }
                });
        }
    };
});